package com.wisnews.controller.sr;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wisnews.pojo.Input;
import com.wisnews.pojo.Result;
import com.wisnews.pojo.sr.entity.AlbumPicture;
import com.wisnews.service.sr.Impl.AlbumPictureServiceImpl;
import com.wisnews.util.Constants;
import com.wisnews.util.FileUploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuyd
 * @since 2021-02-25
 */
@RestController
@RequestMapping("/album-picture")
@Slf4j
public class AlbumPictureController {

    @Autowired
    private AlbumPictureServiceImpl albumPictureService;

    @Value("${custom.parameter.file-upload-path}")
    private String upPath;

    @PostMapping("image")
    public Result uploadOneImage(@RequestParam(value = "file",required = true) MultipartFile imgFile){

        if (imgFile.isEmpty()){
            return new Result(Constants.SYS_CODE.FAIL, Constants.SYS_CODE.FAIL_MESSAGE);
        }

        log.error("文件上传开始！");

        String originalFilename = imgFile.getOriginalFilename();
        int lastIndex = originalFilename.lastIndexOf('.');

        String type = originalFilename.substring(lastIndex);
        String newFileName = FileUploadUtil.generateRandomFileName()+type;
        try {
            imgFile.transferTo(new File(upPath,newFileName));
        }catch (IOException e){
            log.error(e.toString());
            return new Result(Constants.SYS_CODE.FAIL, Constants.SYS_CODE.FAIL_MESSAGE);
        }
        log.warn(originalFilename+"存储文件信息到数据库");

        AlbumPicture picture = new AlbumPicture();

        picture.setPictureUrl("/newsImg/"+newFileName);

        albumPictureService.save(picture);

        log.warn(originalFilename+"文件上传完成！");

        return new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE);
    }


    @PostMapping("images")
    public Result uploadOneImage(@RequestParam(value = "file",required = true) List<MultipartFile> imgFiles){

        int size = imgFiles.size();
        if (size<1){
            return new Result(Constants.SYS_CODE.FAIL, Constants.SYS_CODE.FAIL_MESSAGE);
        }

        imgFiles.forEach(imgFile->{
            log.error("文件上传开始！");

            String originalFilename = imgFile.getOriginalFilename();
            int lastIndex = originalFilename.lastIndexOf('.');

            String type = originalFilename.substring(lastIndex);
            String newFileName = FileUploadUtil.generateRandomFileName()+type;
            try {
                imgFile.transferTo(new File(upPath,newFileName));
            }catch (IOException e){
                log.error(e.toString());
            }
            log.warn(originalFilename+"存储文件信息到数据库");

            AlbumPicture picture = new AlbumPicture();

            picture.setPictureUrl("/newsImg/"+newFileName);

            albumPictureService.save(picture);

            log.warn(originalFilename+"文件上传完成！");});

        return new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE);
    }

    @PostMapping("list")
    public Result pictureList(Input input){

        IPage<AlbumPicture> albumPictures = albumPictureService.querylist(input.toPage());

        return new Result(Constants.SYS_CODE.SUCCESS, Constants.SYS_CODE.SUCCESS_MESSAGE,albumPictures);
    }

}
